const Controller = require('egg').Controller;

class BannerController extends Controller {
  // 创建轮播图
  async create() {
    const { ctx, service } = this;

    try {
      // 参数验证
      const rules = {
        image_url: { type: 'string', required: true },
        display_order: { type: 'number', required: true }
      };
      ctx.validate(rules);

      const bannerData = ctx.request.body;
      const result = await service.banner.create(bannerData);

      ctx.body = {
        code: 0,
        data: result,
        msg: '创建成功'
      };
    } catch (error) {
      if (error.errors) {
        ctx.body = {
          code: 1,
          data: null,
          msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
        };
        return;
      }

      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '创建轮播图失败'
      };
    }
  }

  // 获取轮播图列表
  async list() {
    const { ctx, service } = this;

    try {
      const query = ctx.query;
      const result = await service.banner.getBanners(query);

      ctx.body = {
        code: 0,
        data: result,
        msg: '获取成功'
      };
    } catch (error) {
      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '获取轮播图列表失败'
      };
    }
  }

  // 获取轮播图详情
  async detail() {
    const { ctx, service } = this;

    try {
      const { id } = ctx.params;

      // 验证ID参数
      const rules = {
        id: { type: 'id', required: true }
      };
      ctx.validate(rules, ctx.params);

      const banner = await service.banner.getBannerById(id);

      ctx.body = {
        code: 0,
        data: banner,
        msg: '获取成功'
      };
    } catch (error) {
      if (error.errors) {
        ctx.body = {
          code: 1,
          data: null,
          msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
        };
        return;
      }

      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '获取轮播图详情失败'
      };
    }
  }

  // 更新轮播图信息
  async update() {
    const { ctx, service } = this;

    try {
      const { id } = ctx.params;
      const updateData = ctx.request.body;

      // 验证路径参数
      const paramRules = {
        id: { type: 'id', required: true }
      };
      ctx.validate(paramRules, ctx.params);

      // 验证body参数
      const bodyRules = {
        image_url: { type: 'string', required: false },
        link_url: { type: 'string', required: false },
        title: { type: 'string', required: false },
        display_order: { type: 'number', required: false },
        is_active: { type: 'number', required: false }
      };
      ctx.validate(bodyRules);

      const result = await service.banner.updateBanner(id, updateData);

      ctx.body = {
        code: 0,
        data: result,
        msg: '更新成功'
      };
    } catch (error) {
      if (error.errors) {
        ctx.body = {
          code: 1,
          data: null,
          msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
        };
        return;
      }

      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '更新轮播图失败'
      };
    }
  }

  // 删除轮播图
  async delete() {
    const { ctx, service } = this;

    try {
      const { id } = ctx.params;

      // 验证ID参数
      const rules = {
        id: { type: 'id', required: true }
      };
      ctx.validate(rules, ctx.params);

      await service.banner.deleteBanner(id);

      ctx.body = {
        code: 0,
        data: null,
        msg: '删除成功'
      };
    } catch (error) {
      if (error.errors) {
        ctx.body = {
          code: 1,
          data: null,
          msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
        };
        return;
      }

      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '删除轮播图失败'
      };
    }
  }

  // 获取启用的轮播图
  async active() {
    const { ctx, service } = this;

    try {
      const banners = await service.banner.getActiveBanners();

      ctx.body = {
        code: 0,
        data: banners,
        msg: '获取成功'
      };
    } catch (error) {
      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '获取启用轮播图失败'
      };
    }
  }

  // 切换轮播图状态
  async toggleStatus() {
    const { ctx, service } = this;

    try {
      const { id } = ctx.params;

      // 验证ID参数
      const rules = {
        id: { type: 'id', required: true }
      };
      ctx.validate(rules, ctx.params);

      const result = await service.banner.toggleBannerStatus(id);

      ctx.body = {
        code: 0,
        data: result,
        msg: '状态切换成功'
      };
    } catch (error) {
      if (error.errors) {
        ctx.body = {
          code: 1,
          data: null,
          msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
        };
        return;
      }

      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '切换轮播图状态失败'
      };
    }
  }

  // 批量更新轮播图顺序
  async batchUpdateOrders() {
    const { ctx, service } = this;

    try {
      const rules = {
        orders: { type: 'array', required: true }
      };
      ctx.validate(rules);

      const { orders } = ctx.request.body;

      // 验证orders数组中的每个对象
      for (const order of orders) {
        const orderRules = {
          banner_id: { type: 'number', required: true },
          display_order: { type: 'number', required: true }
        };
        ctx.validate(orderRules, order);
      }

      const result = await service.banner.updateBannerOrders(orders);

      ctx.body = {
        code: 0,
        data: result,
        msg: `批量更新完成，成功: ${result.success}, 失败: ${result.failed}`
      };
    } catch (error) {
      if (error.errors) {
        ctx.body = {
          code: 1,
          data: null,
          msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
        };
        return;
      }

      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '批量更新轮播图顺序失败'
      };
    }
  }

  // 获取轮播图统计
  async stats() {
    const { ctx, service } = this;

    try {
      const stats = await service.banner.getBannerStats();

      ctx.body = {
        code: 0,
        data: stats,
        msg: '获取成功'
      };
    } catch (error) {
      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '获取轮播图统计失败'
      };
    }
  }

  // 批量删除轮播图
  async batchDelete() {
    const { ctx, service } = this;

    try {
      const rules = {
        ids: { type: 'array', required: true }
      };
      ctx.validate(rules);

      const { ids } = ctx.request.body;

      const results = {
        success: 0,
        failed: 0,
        errors: []
      };

      for (const id of ids) {
        try {
          await service.banner.deleteBanner(id);
          results.success++;
        } catch (error) {
          results.failed++;
          results.errors.push(`轮播图ID ${id}: ${error.message}`);
        }
      }

      ctx.body = {
        code: 0,
        data: results,
        msg: `批量删除完成，成功: ${results.success}, 失败: ${results.failed}`
      };
    } catch (error) {
      if (error.errors) {
        ctx.body = {
          code: 1,
          data: null,
          msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
        };
        return;
      }

      ctx.body = {
        code: 1,
        data: null,
        msg: error.message || '批量删除失败'
      };
    }
  }
}

module.exports = BannerController;